package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourceInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-03-10
 */
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    //课程列表TODO
    //完善条件查询带分页
//    @GetMapping("pageCourseCondition/{page}/{limit}")
//    public R getCourseList(@PathVariable long page,@PathVariable long limit,@RequestBody(required = false) CourseQuery searchObj){
//        Page<EduCourse> pageCourse = new Page<>(page, limit);
//        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
//        String title = searchObj.getTitle();
//        String subjectId = searchObj.getSubjectId();
//        String subjectParentId = searchObj.getSubjectParentId();
//        String teacherId = searchObj.getTeacherId();
//        if (!StringUtils.isEmpty(title)){
//            wrapper.like("title",title);
//        }
//        if (!StringUtils.isEmpty(subjectId)){
//            wrapper.eq("subject_id",subjectId);
//        }
//        if (!StringUtils.isEmpty(subjectParentId)){
//            wrapper.eq("subject_parent_id",subjectParentId);
//        }
//        if (!StringUtils.isEmpty(teacherId)){
//            wrapper.eq("teacher_id",teacherId);
//        }
////        List<EduCourse> list = courseService.list(wrapper);
////        return R.ok().data("list",list);
//       courseService.page(pageCourse, wrapper);
//        long total = pageCourse.getTotal();
//        List<EduCourse> records = pageCourse.getRecords();
//        return  R.ok().data("total",total).data("records",records);
//
//    }

    @ApiOperation(value = "分页课程列表")
    @GetMapping("pageCourseCondition/{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)         @PathVariable Long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)                 CourseQuery courseQuery)
    {
        Page<EduCourse> pageParam = new Page<>(page, limit);
        courseService.pageQuery(pageParam, courseQuery);
        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return  R.ok().data("total", total).data("rows", records); }

    //添加课程基本信息的方法
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourceInfoVo courceInfoVo){

        //返回添加之后课程id，为了后面添加大纲使用
        String id =courseService.saveCourceInfo(courceInfoVo);
        return R.ok().data("courseId",id);
    }

    //根据课程id查询课程的基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
       CourceInfoVo courceInfoVo= courseService.getCourseInfo(courseId);
       return R.ok().data("courseInfoVo",courceInfoVo);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourceInfoVo courceInfoVo){
        courseService.updateCourseInfo(courceInfoVo);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo =courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }
  //课程最终发布
    //修改课程状态
    @PostMapping("publishCourseById/{id}")
    public R publishCourseById(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");//设置课程发布状态
        courseService.updateById(eduCourse);
        return  R.ok();
    }

    //删除课程
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId);
        return R.ok();
    }
}

